package com.example.apiproject.service.impl;

import com.example.apiproject.entity.Deals;
import com.example.apiproject.dao.DealsDao;
import com.example.apiproject.service.DealsService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * (Deals)表服务实现类
 *
 * @author makejava
 * @since 2023-09-23 13:40:57
 */
@Service("dealsService")
public class DealsServiceImpl implements DealsService {
    @Resource
    private DealsDao dealsDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Deals queryById(Integer id) {
        return this.dealsDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param deals 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Deals> queryByPage(Deals deals, PageRequest pageRequest) {
        long total = this.dealsDao.count(deals);
        return new PageImpl<>(this.dealsDao.queryAllByLimit(deals, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param deals 实例对象
     * @return 实例对象
     */
    @Override
    public Deals insert(Deals deals) {
        this.dealsDao.insert(deals);
        return deals;
    }

    /**
     * 修改数据
     *
     * @param deals 实例对象
     * @return 实例对象
     */
    @Override
    public Deals update(Deals deals) {
        this.dealsDao.update(deals);
        return this.queryById(deals.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.dealsDao.deleteById(id) > 0;
    }
}
